#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <regex>
using namespace std;

void splitByBlank(string resource)
{
	stringstream strstr(resource);
	string word;
	while (strstr >> word)
	{
		cout << word << endl;
	}
}
void splitByChar(string resource, const char delimiter)
{
	stringstream strstr(resource);
	string word;
	while (getline(strstr, word, delimiter))
	{
		cout << word << endl;
	}
}
void splitByString(string resource, const string delimiter)
{
	int start = 0;
	int end = 0;
	do
	{
		end = resource.find(delimiter, start);
		cout << resource.substr(start, end - start) << endl;
		start = end + delimiter.size();
	} while (end != -1);
}
void splitByRegex(string resource, const string delimiterRegex)
{
	regex ws_re(delimiterRegex);
	vector<string> list(sregex_token_iterator(resource.begin(), resource.end(), ws_re, -1), sregex_token_iterator());
	for (auto i : list)
	{
		cout << i << endl;
	}
}
void splitMultiByString(string resource, const string delimiters)
{
	int current, next = -1;
	do
	{
		current = next + 1;
		next = resource.find_first_of(delimiters, current);
		string i = resource.substr(current, next - current);
		cout << i << endl;
	} while (next != string::npos);
}
std::vector<std::string> splitByString(const std::string &source, const std::string &delimiters)
{
    int current, next = -1;
    std::string dst;
    std::vector<std::string> elems;
    do
    {
        current = next + 1;
        next = source.find_first_of(delimiters, current);
        elems.emplace_back(source.substr(current, next - current));
    } while (next != string::npos);
    return elems;
}
std::string getPrevString(const std::string &source, const std::string &delimiters)
{
    std::string prev;
    std::vector<std::string> elems = splitByString(source, delimiters);
    if (elems.size() > 0)
    {
        prev = elems.front();
    }
    return prev;
}
std::vector<std::string> sepstr(const std::string &sStr, const std::string &delimiter, bool withEmpty = false)
{
    std::vector<std::string> vList;
    std::string::size_type pos = 0;
    std::string::size_type next = 0;
    while ((next = sStr.find(delimiter, pos)) != std::string::npos) {
        std::string token = sStr.substr(pos, next - pos);
        vList.emplace_back(token);
        pos = next + delimiter.length();
    }
    // 最后再把结尾追加到vector
    vList.emplace_back(sStr.substr(pos));
    return vList;
}
int main(int argc, char const *argv[])
{
	string str = "How do you do!";
	splitByString(str, " ");
	cout << endl;
	return 0;
}
